package com.cim120.commons.cip.utils;

import org.apache.commons.lang3.StringUtils;

import static com.cim120.commons.cip.constant.ImageConstant.*;


/**
 * Created by tonydeng on 16/4/27.
 */
public class ImageUtils {
    /**
     * 判断是否为默认的图片扩展名
     * @param extension
     * @return
     */
    public static boolean isDefaultExtension(String extension){
        return (StringUtils.isNotEmpty(extension) && DEFAULT_FORMAT.equals(extension));
    }
    /**
     * 获得文件扩展名
     *
     * @param path
     * @return file extension
     * @author tonydeng
     */
    public static String getExtension(String path) {
        String extension = null;
        if ((path != null) && (path.length() > 0)) {
            int i = path.lastIndexOf('.');
            if ((i > -1) && ((path.length() - i) <= 4)) {
                extension = path.substring(i + 1);
            }
        }
        return getDefaultExtension(extension);
    }


    /**
     * 获取默认的图片扩展名，除jpg、gif、webp以为，都设置成jpg
     *
     * @param extension
     * @return
     */
    public static String getDefaultExtension(String extension) {
        if (StringUtils.isEmpty(extension))
            return DEFAULT_FORMAT;

        switch (extension.toLowerCase()) {
            case JPG:
                return JPG;
            case PNG:
                return JPG;
            case GIF:
                return GIF;
            case JPEG:
                return JPG;
            case BMP:
                return JPG;
            case WEBP:
                return WEBP;
            default:
                return DEFAULT_FORMAT;
        }
    }
}
